	//
	//  GaoButtonLiiOne.m
	//
	//  Created by Gikki Ares on 2020/12/8.
	//

#import "JsButtonLiiOne.h"
#import "JsGeometryManager.h"

@interface JsButtonLiiOne()
{
	UILabel *mlb;
	UIImageView *miv;
	UIImageView *miv_background;
	UIButton * mbtn;
	int mi_setupUiCount;
	
	void(^mblock_position)(void);
	
}

@end


@implementation JsButtonLiiOne

#pragma mark LifeCycle
	//initWighFrame时,就确保传入正确的UI数据.
- (instancetype)initWithFrame:(CGRect)frame
{
	self = [super initWithFrame:frame];
	if (self) {
		[self commonInit];
	}
	return self;
}

- (UIImageView *)imageView {
	if(!miv) {
		miv = [UIImageView new];
		[self addSubview:miv];
	}
	return miv;
}

- (UILabel *)label {
	if(!mlb) {
	mlb = [UILabel new];
	[self addSubview:mlb];
	}
	return mlb;
}

- (instancetype)initWithCoder:(NSCoder *)coder {
	if(self == [super initWithCoder:coder]) {
		
	}
	return self;
}
#pragma mark 2 Interface
-(void)addTarget:(id)target action:(SEL)sel {
	[mbtn addTarget:target action:sel forControlEvents:UIControlEventTouchUpInside];
}

- (void)awakeFromNib {
	[super awakeFromNib];
	[self commonInit];
}

- (void)commonInit {
	
//	if(CGSizeEqualToSize(self.frame.size, CGSizeZero)) {
//		self.frame = CGRectMake(0, 0,self.vm.size.width,self.vm.size.height);
//	}
//	else {
//		self.vm.size = self.frame.size;
//	}
	_vm = [JsButtonLiiVm new];
	mbtn = [[UIButton alloc]init];
	[mbtn addTarget:self  action:@selector(onClick:) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:mbtn];
	[self setupUi];
	
}

- (void)setFrame:(CGRect)frame {
	[super setFrame:frame];
}



	//自己的frame还是交给父视图去控制.
	//layoutSubview只负责直接子视图的布局.
- (void)layoutSubviews {
	[super layoutSubviews];
	if(mblock_position) {
		mblock_position();
		mblock_position = nil;
	}
}


- (void)setVm:(JsButtonLiiVm *)vm {
	_vm = vm;
	[self setupUi];
}


- (void)setupUiWithVm:(JsButtonLiiVm *) vm {
//	NSLog(@"Update Ui.");
	mi_setupUiCount = 0;
	{
			//设置前景图
		if(vm.foregroundImage) {
			if(!miv) {
				miv = [UIImageView new];
				[self addSubview:miv];
			}
			miv.image = vm.foregroundImage;
			miv.hidden = NO;
		}
		CGRect foregroundImageViewFrame = vm.foregroundImageViewFrame;
		if(CGRectIsNull(foregroundImageViewFrame)) {
			foregroundImageViewFrame = self.bounds;
		}
		miv.frame = foregroundImageViewFrame;
		if(vm.isForegroundImageCircle) {
			CGFloat minSize = MIN(miv.bounds.size.width,miv.bounds.size.height);
			miv.layer.cornerRadius = minSize/2;
			//MARK: 单独开启圆角并不需要开启masksToBounds.
//			miv.layer.masksToBounds = YES;
		}
	}
	
	{
			//设置label
			//Create if needed
		if(vm.text||vm.atext) {
			if(!mlb) {
				mlb = [UILabel new];
				[self addSubview:mlb];
			}
		}
			//设置frame
		CGRect labelFrame = vm.labelFrame;
		if(CGRectIsNull(labelFrame)) {
			labelFrame = self.bounds;
		}
		mlb.frame = labelFrame;
		
		mlb.textColor = vm.labelTextColor;
		mlb.font = vm.labelFont;
		mlb.textAlignment = vm.mTextAlignment;
		
			//优先显示富文本,没有再显示文本.
		if(vm.atext) {
			mlb.attributedText = vm.atext;
		}
		else {
			mlb.text = vm.text;
		}
	}
	
	self.backgroundColor = vm.backgroundColor;
	self.layer.masksToBounds = NO;
	
	if(vm.isCircle) {
		CGFloat minSize = MIN(self.bounds.size.width,self.bounds.size.height);
		self.layer.cornerRadius = minSize/2;
		self.layer.masksToBounds = YES;
	}
	if(vm.cornerRaidus) {
		self.layer.cornerRadius = vm.cornerRaidus;
		self.layer.masksToBounds = YES;
	}
	
	
	if(vm.borderWidth) {
		self.layer.borderWidth = vm.borderWidth;
		self.layer.borderColor = vm.borderColor.CGColor;
	}
	{
			//btn
		CGRect frame = vm.btnFrame;
		if(CGRectIsNull(frame)) {
			frame = self.bounds;
		}
		mbtn.frame = frame;
		mbtn.backgroundColor = [UIColor clearColor];
	}
	[self bringSubviewToFront:mbtn];
}

#pragma mark 2 BasicFunction

#pragma mark 3 Interface
	//设置背景图片
- (void)setBackgroundImage:(UIImage *)image {
	miv_background.image = image;
	miv_background.hidden = NO;
}

- (UIFont *)textFont {
	return self.vm.labelFont;
}


- (void)setTitle:(NSString *)title {
	[self setTitle:title withFont:self.vm.labelFont];
}

- (void)setTitle:(NSString *)title withFont:(UIFont *)font {
	[self setTitle:title withFont:font textColor:self.vm.labelTextColor];
}

- (void)setTitle:(NSString *)title withFont:(UIFont *)font textColor:(UIColor *)textColor {
	if(!mlb) {
		mlb = [UILabel new];
		[self addSubview:mlb];
	}
	mlb.text = title;
	mlb.font = font;
	mlb.textColor = textColor;
	
	self.vm.text = title;
	self.vm.labelFont = font;
	self.vm.labelTextColor = textColor;
	self.vm.atext = nil;
	[self setupUi];
}


//
//- (void)setMb_isCircleImage:(BOOL)mb_isCircleImage {
//	if(mb_isCircleImage) {
//		CGFloat minSize = MIN(self.vm.imageViewFrame.size.width,self.vm.imageViewFrame.size.height);
//		miv.layer.cornerRadius = minSize/2;
//		miv.layer.masksToBounds = YES;
//	}
//	else {
//		miv.layer.cornerRadius = 0;
//		miv.layer.masksToBounds = NO;
//	}
//}

- (void)setImage:(UIImage *)image {
	self.vm.foregroundImage = image;
	[self setupUi];
}


	//更新所有的内容,而layoutSubview,只是影响布局
	//没有就创建.所有的
	//先不使用layoutsubviews试试,看.
- (void)setupUi {
	mi_setupUiCount ++;
	JsButtonLiiVm * vm = self.vm;
		//当1个runloop有多次更新请求时,只触发一次.
	if(mi_setupUiCount == 1) {
		dispatch_async(dispatch_get_main_queue(), ^{
			[self setupUiWithVm:vm];
		});
	}
}

- (void)updateFontColor:(UIColor *)color {
	self.vm.labelTextColor = color;
	[self setupUi];
}

- (void)updateAttributedText:(NSAttributedString *)astr {
	self.vm.atext = astr;
	[self setupUi];
}

#pragma mark 2 interface

- (void)updateLabelAlignment:(NSTextAlignment)textAlignment {
	self.vm.mTextAlignment = textAlignment;
	[self setupUi];
}




#pragma mark 5 Event
- (IBAction)onClick:(id)sender {
	//调用回调.
	if(self.clickCallback) {
		self.clickCallback();
	}
	//调用代理.
	if(_delegate &&[_delegate respondsToSelector:@selector(jsButtonLiiOneClicked:)]){
		[_delegate jsButtonLiiOneClicked:self];
	}
}
@end


